package com.whf.android.jar.base.latte;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.whf.android.jar.app.Latte;


/***
 * 基类
 * @author qf
 */
public abstract class BaseFragment extends Fragment implements ActivityResultCallback<ActivityResult> {

    private static final String TAG_OFFSET = "TAG_OFFSET";
    private static final int KEY_OFFSET = -123;
    protected Context context;
    protected View mRootView = null;

    /*** 跳转界面回调刷新器 */
    protected ActivityResultLauncher<Intent> startLauncher;

    /**
     * 布局文件
     *
     * @return Object
     */
    protected abstract Object setLayout();

    /**
     * 初始化
     *
     * @param savedInstanceState:Bundle
     * @param rootView:主view
     */
    protected abstract void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView);


    public <T extends View> T findViewById(@IdRes int viewId) {
        if (mRootView != null) {
            return mRootView.findViewById(viewId);
        }
        throw new NullPointerException("rootView is null");
    }

    protected View getView(@IdRes int viewId) {
        return findViewById(viewId);
    }

    protected TextView getTextView(@IdRes int viewId) {
        return findViewById(viewId);
    }

    protected ImageView getImageView(@IdRes int viewId) {
        return findViewById(viewId);
    }

    protected EditText getEditText(@IdRes int viewId) {
        return findViewById(viewId);
    }

    protected Button getButton(@IdRes int viewId) {
        return findViewById(viewId);
    }

    protected RadioButton getRadioButton(@IdRes int viewId) {
        return findViewById(viewId);
    }


    //******************************************************************************//

    /**
     * Add the top margin size equals status bar's height for view.
     *
     * @param view The view.(只能使用，不建议重构)
     */
    protected void addMarginTopEqualStatusBarHeight(View view) {
        if (view == null) {
            return;
        }
        view.setTag(TAG_OFFSET);
        Object haveSetOffset = view.getTag(KEY_OFFSET);
        if (haveSetOffset != null && (Boolean) haveSetOffset) {
            return;
        }
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin,
                layoutParams.topMargin + getStatusBarHeight(),
                layoutParams.rightMargin,
                layoutParams.bottomMargin);
        view.setTag(KEY_OFFSET, true);
    }

    private int getStatusBarHeight() {
        int resourceId = Latte.getBarHeight();
        return resourceId == -1 ? 36 : resourceId;
    }

    //******************************************************************************//

    /************************************* 尺寸转换  *****************************************/


    protected int dp2px(float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    protected int sp2px(float sp) {
        final float scale = this.context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (sp * scale + 0.5f);
    }

    /************************************* 设置获取值  *****************************************/

    protected String getTagStr(int viewId) {
        if (getTextView(viewId) == null) {
            return "";
        }
        if (getTextView(viewId).getTag() == null) {
            return "";
        }
        return String.valueOf(getTextView(viewId).getTag()).trim();
    }

    protected String getHintStr(int viewId) {
        if (getTextView(viewId) == null) {
            return "";
        }
        if (getTextView(viewId).getHint() == null) {
            return "";
        }
        return String.valueOf(getTextView(viewId).getHint()).trim();
    }

    protected String getTextStr(int viewId) {
        if (getTextView(viewId) == null) {
            return "";
        }
        if (getTextView(viewId).getText() == null) {
            return "";
        }
        return getTextView(viewId).getText().toString().trim();
    }

    /***
     * 判断数据是否为空
     * @param viewId：
     */
    protected boolean isTextEmpty(int viewId) {
        return isTextEmpty(viewId, getHintStr(viewId));
    }

    /**
     * 判断数据是否为空
     *
     * @param viewId：
     * @param text：
     */
    protected boolean isTextEmpty(int viewId, final CharSequence text) {
        TextView depText = getTextView(viewId);
        if (depText != null && getTextStr(viewId).isEmpty()) {
            ToastUtils.showShort(text);
            return true;
        }
        return false;
    }

    /************************************* 设置值 *****************************************/

    protected final void setText(@IdRes int viewId, CharSequence text) {
        TextView textView = getTextView(viewId);
        if (textView != null) {
            textView.setText(text);
        }
    }

    protected final void setTextHint(int viewId, CharSequence hint) {
        TextView textView = getTextView(viewId);
        if (textView != null) {
            textView.setHint(hint);
        }
    }

    protected final void setTextInputType(int viewId, int type) {
        TextView view = findViewById(viewId);
        if (view != null) {
            view.setInputType(type);
            view.setSingleLine(false);
        }
    }

    /***
     * 设置字体大小
     * @param viewId：
     * @param size：
     */
    protected final void setTextSize(@IdRes int viewId, float size) {
        TextView textView = getTextView(viewId);
        if (textView != null) {
            textView.setTextSize(SizeUtils.sp2px(size));
        }
    }

    protected final void setTextStyle(@IdRes int viewId, int style) {
        //Typeface.NORMAL Typeface.BOLD Typeface.ITALIC
        TextView textView = getTextView(viewId);
        if (textView != null) {
            textView.setTypeface(null, style);
        }
    }

    protected final void setTextTag(@IdRes int viewId, CharSequence text) {
        TextView textView = getTextView(viewId);
        if (textView != null) {
            textView.setTag(text);
        }
    }

    protected final void setTextColor(@IdRes int viewId, @ColorInt int textColor) {
        TextView textView = getTextView(viewId);
        if (textView != null) {
            textView.setTextColor(textColor);
        }
    }

    protected final void setBackgroundResource(@IdRes int viewId, @DrawableRes int resId) {
        View view = getView(viewId);
        if (view != null) {
            view.setBackgroundResource(resId);
        }
    }

    protected final void setImageResource(int viewId, @DrawableRes int text) {
        ImageView view = getImageView(viewId);
        if (view != null) {
            view.setImageResource(text);
        }
    }


    protected final void setRadioButton(@IdRes int viewId, CharSequence text) {
        RadioButton textView = getRadioButton(viewId);
        if (textView != null) {
            textView.setText(text);
        }
    }

    /**
     * @param viewId:控件选中情况交换
     */
    protected final void setSelect(int viewId) {
        setSelected(viewId, !isSelected(viewId));
    }

    /**
     * @param viewId:设置控件是否选中
     * @param text:设置控件是否选中
     */
    protected final void setSelected(int viewId, boolean text) {
        View view = getView(viewId);
        if (view != null) {
            view.setSelected(text);
        }
    }

    /**
     * @param viewId:获得控件是否选中
     */
    protected boolean isSelected(int viewId) {
        View view = getView(viewId);
        if (view != null) {
            return view.isSelected();
        }
        return false;
    }

    /************************************* 设置是否可以编辑  *****************************************/

    protected final void setViewEdit(int viewId, boolean isEdit) {
        TextView view = getTextView(viewId);
        if (view != null) {
            //去掉点击时编辑框下面横线:
            view.setEnabled(isEdit);
            //不可编辑
            view.setFocusable(isEdit);
            //不可编辑
            view.setFocusableInTouchMode(isEdit);
        }
    }

    protected final void setViewOnClick(int viewId, boolean isEdit) {
        View view = findViewById(viewId);
        if (view != null) {
            //去掉点击时编辑框下面横线:
            view.setEnabled(isEdit);
            //不可编辑
            view.setFocusable(isEdit);
        }
    }

    //************************************* 设置控件显示  *****************************************/


    /**
     * @param viewId:需要设置的控件。
     * @param show:true显示；false隐藏
     */
    protected final void setShow(@IdRes int viewId, boolean show) {
        View view = findViewById(viewId);
        if (view != null) {
            setShow(view, show);
        }
    }

    /**
     * @param view:需要设置的控件。
     * @param show:true显示；false隐藏
     */
    protected final void setShow(View view, boolean show) {
        if (view != null) {
            view.setVisibility(show ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * @param viewId:需要设置的控件。
     * @param hide:true显示；false不显示
     */
    protected final void setHide(@IdRes int viewId, boolean hide) {
        View view = findViewById(viewId);
        if (view != null) {
            setHide(view, hide);
        }
    }

    /**
     * @param view:需要设置的控件。
     * @param show:true显示；false隐藏
     */
    protected final void setHide(View view, boolean show) {
        if (view != null) {
            view.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
        }
    }


    /************************************* 设置事件  *****************************************/

    protected final void setOnClickListener(@IdRes int viewId, View.OnClickListener listener) {
        View view = findViewById(viewId);
        if (view != null) {
            view.setOnClickListener(listener);
        }
    }

    /**
     * @param viewId:设置输入监听事件
     * @param watcher:
     */
    protected final void addTextChangedListener(int viewId, TextWatcher watcher) {
        TextView view = findViewById(viewId);
        if (view != null) {
            view.addTextChangedListener(watcher);
        }
    }

    /**
     * @param viewId:设置判断控件监听事件
     * @param listener:
     */
    protected final void setOnCheckedSwitchListener(int viewId, CompoundButton.OnCheckedChangeListener listener) {
        @SuppressLint("UseSwitchCompatOrMaterialCode")
        Switch swtTest = findViewById(viewId);
        if (swtTest != null) {
            //给开关按钮设置监听状态改变事件
            swtTest.setOnCheckedChangeListener(listener);
        }
    }

    /**
     * @param viewId:设置RadioGroup监听事件
     * @param watcher:
     */
    protected final void setOnCheckedChangeListener(int viewId, RadioGroup.OnCheckedChangeListener watcher) {
        RadioGroup view = findViewById(viewId);
        if (view != null) {
            view.setOnCheckedChangeListener(watcher);
        }
    }

    @Nullable
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        final View rootView;
        if (setLayout() instanceof Integer) {
            rootView = inflater.inflate((int) setLayout(), container, false);
        } else if (setLayout() instanceof View) {
            rootView = (View) setLayout();
        } else {
            throw new NullPointerException("viewBinding is null");
        }
        startLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), this);
        if (rootView != null) {
            mRootView = rootView;
            context = getContext();
            onBindView(savedInstanceState, rootView);
        }
        return rootView;
    }

    @Override
    public void onActivityResult(ActivityResult result) {

    }

}
